<?php

namespace App\Http\Controllers\Admin;

use App\Type;
use Validator;
use App\Article;
use App\Category;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{
    public function index(Request $request)
    {
        $articles = Article::orderBy('updated_at', 'desc')->get();
        return view('admin.article.index', [
            'request' => $request,
            'articles' => $articles
        ]);
    }

    public function add(Request $request)
    {
        if ($request->isMethod('post')) {
            $data = $request->except(['_token']);
            $rules = [
                'title' => 'required|min:2',
                'category_id' => 'required|integer'
            ];
            $messages = [
                'title.required' => '请输入标题！',
                'title.min' => '标题至少两个字符！',
                'category_id.required' => '请选择栏目！',
                'category_id.integer' => '栏目必须为整数'
            ];
            $validator = Validator::make($data, $rules, $messages);
            if ($validator->fails()) {
                return redirect()->back()->withErrors($validator)->withInput();
            }
            $article = new Article();
            $article->category_id = $data['category_id'];
            $article->title = $data['title'];
            $article->summary = $data['summary'];
            $article->content = $data['content'];
            $article->save();
            return redirect('admin/article');
        }
        $categorys = Category::orderBy('fullpath', 'asc')->get();
        $factories = Type::where('parent_id', '=', '0')->get();
        return view('admin.article.add', [
            'request' => $request,
            'categorys' => $categorys,
            'factories' => $factories
        ]);
    }

    public function edit(Request $request, $id)
    {
        if ($request->isMethod('post')) {
            $data = $request->except(['_token']);
            $rules = [
                'title' => 'required|min:2',
                'category_id' => 'required|integer'
            ];
            $messages = [
                'title.required' => '请输入标题！',
                'title.min' => '标题至少两个字符！',
                'category_id.required' => '请选择栏目！',
                'category_id.integer' => '请选择栏目'
            ];
            $validator = Validator::make($data, $rules, $messages);
            if ($validator->fails()) {
                return redirect()->back()->withErrors($validator)->withInput();
            }
            $article = Article::find($data['id']);
            $article->category_id = $data['category_id'];
            $article->title = $data['title'];
            $article->summary = $data['summary'];
            $article->content = $data['content'];
            $article->save();
            return redirect('admin/article');
        }
        $article = Article::find($id);
        if (!isset($article->title)) {
            return redirect('admin/article');
        }
        $categorys = Category::orderBy('fullpath', 'asc')->get();
        return view('admin.article.edit', [
            'request' => $request,
            'categorys' => $categorys,
            'article' => $article
        ]);
    }

    public function del(Request $request)
    {
        $id = $request->input('id');
        $article = Article::find($id);
        if ($article->delete()) {
            $return = ['code' => 0];
        } else {
            $return = ['code' => __LINE__, 'msg' => '删除失败'];
        }
        return response()->json($return);
    }
}